//
//  AppDelegate.swift
//  kingWeiBo
//
//  Created by King on 15/9/6.
//  Copyright © 2015年 King. All rights reserved.
//

import UIKit

let  KNTongzhi = "KNTongzhi"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "qiehuanVC:", name: KNTongzhi, object: nil)

        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultVC()
        window?.makeKeyAndVisible()
        return true
    }
    
    func qiehuanVC(n:NSNotification){
    
        let mainVC = n.object as! Bool
        window?.rootViewController = mainVC ? MainTVC() : WellcomViewController()
    }
    
    func defaultVC()-> UIViewController{
        if !userAccount.userLogin{
            
            return MainTVC()
        }
        
        return isNewVision() ? NewFeatureVC() : WellcomViewController()
    
    }
    
    
    func isNewVision()->Bool{
        
        // 获取当前版本
        let currentVision = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!

        // 获取旧版本
        let sanBoxVisionKey = "sanBoxVisionKey"
        let sanBoxVision = NSUserDefaults.standardUserDefaults().doubleForKey(sanBoxVisionKey)
        NSUserDefaults.standardUserDefaults().setDouble(currentVision, forKey: sanBoxVisionKey)
        
        //同步
        NSUserDefaults.standardUserDefaults().synchronize()
        
        
        // 比较
        return currentVision > sanBoxVision
    }

}

